#!/bin/sh
alias GETTEXT='gettext "kylin-system-verify"'

PREREQS=""

prereqs() { echo "$PREREQS"; }

case "$1" in
    prereqs)
    prereqs
    exit 0
    ;;
esac


user=$(whoami)

if [ $user != "root" ]; then
	: '
	#echo "请使用sudo或切换到root用户执行该程序"
	'
	echo "$(GETTEXT "Use sudo or switch to root to execute the program")"
	exit 1;
fi

SERIAL_NO=$(grep "^SERIAL:[0-9][0-9]*$" /etc/LICENSE | head -n 1 | awk -F":" '{print $2}')
SERIAL_LENGTH=${#SERIAL_NO}
ROOTFS_MOUNT=$(awk '!/^rootfs / {if ($2 == "/") {print $1 " " $2 " " $3 " " $4; exit}}' /proc/mounts)
ROOTFS_OPTIONS=$(echo $ROOTFS_MOUNT | awk '{ print $4 }')
ROOTFS_FSTYPE=$(echo $ROOTFS_MOUNT | awk '{ print $3 }')
ROOTFS_DEVICE=$(echo $ROOTFS_MOUNT | awk '{ print $1 }')
echo $ROOTFS_OPTIONS | grep -qe '^ro,' && readonly="y" || readonly="n"

export ROOTFS_DEVICE

CODE_NO_HYPHEN="y"
export CODE_NO_HYPHEN

KY_SERIAL_NUMBER=

stay_message() {
	msg="${1}"

	echo ""
	echo $msg
	while true
	do
		sleep 100
	done
}

ky_input_serial_retry() {

	if [ $1 = 0 ]; then
		echo ""
		: '
		#echo "请输入服务序列号："
		'
		echo "$(GETTEXT "Please enter the service serial number:")"
	elif [ $1 = 1 ]; then
		: '
		#echo "无效的服务序列号，请重新输入："
		'
		echo "$(GETTEXT "Invalid service serial number, please re-enter:")"
	elif [ $1 = 2 ]; then
		: '
		#echo "服务序列号长度小于8，请重新输入："
		'
		echo "$(GETTEXT "If the length of the service serial number is less than 8, please re-enter:")"
	else
		: '
		#echo "请输入服务序列号："
		'
		echo "$(GETTEXT "Please enter the service serial number:")"
	fi

	read KY_SERIAL_NUMBER
	echo ""

	if [ "x$KY_SERIAL_NUMBER" = "x" ]; then
		ky_input_serial_retry 1
	fi

	echo "$KY_SERIAL_NUMBER" | grep -q "^[0-9][0-9]*$" || ky_input_serial_retry 1

	KY_SERIAL_LENGTH=${#KY_SERIAL_NUMBER}
	if [ $KY_SERIAL_LENGTH -lt 8 ]; then
		ky_input_serial_retry 2
	fi
}

ky_activate_retry() {
	echo ""
	if [ $1 = 1 ] || [ $1 = 2 ] || [ $1 = 3 ]; then
		: '
		#stay_message "无有效LICENSE文件"
		'
		stay_message "$(GETTEXT "No valid LICENSE file exists")"
	elif [ $1 = 4 ]; then
		: '
		#stay_message "无有效麒麟信息文件"
		'
		stay_message "$(GETTEXT "No valid .kyinfo file exists")"
	elif [ $1 = 14 ]; then
		: '
		#stay_message "无有效公钥"
		'
		stay_message "$(GETTEXT "No valid public key")"
	elif [ $1 = 15 ]; then
		: '
		#stay_message "系统时间有问题"
		'
		stay_message "$(GETTEXT "There is a system time problem")"
	elif [ $1 = 16 ]; then
		: '
		#stay_message "GPG验证出现问题"
		'
		stay_message "$(GETTEXT "GPG authentication is faulty")"
	elif [ $1 = 17 ]; then
		: '
		#stay_message "无法获取有效信息"
		'
		stay_message "$(GETTEXT "Unable to obtain valid information")"
	elif [ $1 = 72 ] || [ $1 = 73 ]; then
		: '
		#stay_message "无有效的服务序列号"
		'
		stay_message "$(GETTEXT "No valid service serial number")"
	elif [ $1 = 8 ] || [ $1 = 9 ] || [ $1 = 10 ] || [ $1 = 11 ] || [ $1 = 12 ] || [ $1 = 13 ]; then
		: '
		#echo "请重试："
		'
		echo "$(GETTEXT "Please try again:")"
		read  KY_ACT_CODE
		if [ "x$KY_ACT_CODE" = "x" ]; then
			ky_activate_retry 9
		else
			kylin_activate "$KY_ACT_CODE" "$KY_SERIAL_NUMBER" || ky_activate_retry $?
		fi
	fi
}

ky_activate() {
	echo ""
	: '
	#echo "请输入激活码"
	'
	echo "$(GETTEXT "Please enter the activation code")"
	read KY_ACT_CODE
	echo ""

	if [ "x$KY_ACT_CODE" = "x" ]; then
		ky_activate_retry 9
	else
		kylin_activate "$KY_ACT_CODE" "$KY_SERIAL_NUMBER" || ky_activate_retry $?
	fi
}

show_qrcode() {
	clear
	HW_INFO=$(echo $1)
	SERIAL_CODE=$(kylin_serial)

	if [ "$QRCODE_USE_CMD" = "1" ]; then
		QR_CODE_STR=$(/usr/bin/kylin_qrcode "$SERIAL_CODE" 2>/dev/null)
	fi

	if [ "$QR_CODE_STR" = "" ]; then
		QR_CODE_STR="http://wx.kylinos.cn/qywx/distro/activate?f=${SERIAL_CODE}&y=${HW_INFO}&p=${SERIAL_NO}"
	fi

	if [ "$QRCODE_TYPE" = "" ]; then
		QRCODE_TYPE="ANSI"
	fi

#	clear
	qrencode -t "$QRCODE_TYPE" -m 2 "${QR_CODE_STR}" 2> /dev/null && \
		: '
		#echo "注册信息二维码" && \
		'
		echo "$(GETTEXT "Registration information TWO-DIMENSIONAL code")" && \
		sleep 2 && \
		: '
		#echo "请按回车键继续..." && \
		'
		echo "$(GETTEXT "Press Enter to continue...")" && \
		read TMP || \
		sleep 0.1
}

ky_gen_register_retry() {
	clear
	: '
	#kylin_encrypted_hardware_info  > /dev/null || stay_message "硬件信息生成失败"
	'
	kylin_encrypted_hardware_info  > /dev/null || stay_message "$(GETTEXT "Failed to generate hardware information")"
	HW_INFO=$(kylin_encrypted_hardware_info)
	show_qrcode "$HW_INFO"

	ky_input_serial_retry 0

	clear
	CODE_NO_HYPHEN="n"
	REGISTER_NO=$(kylin_gen_register "$KY_SERIAL_NUMBER")
	
	echo "$REGISTER_NO $KY_SERIAL_NUMBER"

	: '
	#echo "以上是注册码和序列号，请联系麒麟以获取激活码"
	'
	echo "$(GETTEXT "Above is the registration code and serial number, please contact Kirin for activation code")"

	ky_activate
}

ky_gen_register() {

	echo ""
	sleep 2
	: '
	#echo "请按回车键继续..."
	'
	echo "$(GETTEXT "Press Enter to continue...")"
	read TMP

	clear

	ky_gen_register_retry
}

ky_verify() {
	clear
	if [ ${readonly} = y ]; then
		: '
		#echo "文件系统只读，无法进行验证。"
		'
		echo "$(GETTEXT "The file system is read-only and cannot be verified")"
		exit 1
	fi

	kylin_activation_check
	if [ $? -ne 0 ];then
		exit 1
	fi

	ky_gen_register

}

if [ $SERIAL_LENGTH -eq "7" ]; then
	ky_verify
else
	: '
	#stay_message "LICENSE文件无有效服务序列号"
	'
	stay_message "$(GETTEXT "The LICENSE file does not have a valid service serial number")"
fi

unset ROOTFS_DEVICE
unset CODE_NO_HYPHEN
